//
//  DYLocationViewController.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/3/9.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit

class DYLocationViewController: SYBaseViewController {

    var model: DYLocationChoiceModel!
    
    var searchView: DYSearchView!
    
    let note: DYMSWentiView = {
        let v = DYMSWentiView()
        
        return v
    }()
    
    let jiedao: UILabel = {
        let v = UILabel()
        v.text = "街道"
        v.font = 11.autoFontSize()
        v.textColor = UIColor(hex: 0x333333)
        return v
    }()
    
    var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = Constant.bgViewColor
        return v
    }()
    
    var secLineView: UIView = {
        let v = UIView()
        v.backgroundColor = Constant.bgViewColor
        return v
    }()
    
    var quxian: UIButton = {
        let v = UIButton()
        
        v.setTitleColor(UIColor(hex: 0x3E3E3E), for: .normal)
        v.titleLabel?.font = 12.autoFontSize()
        v.layer.cornerRadius = 4
        v.backgroundColor = UIColor(hex: 0xF4F4F4)
        return v
    }()
    
    let current: UILabel = {
        let v = UILabel()
        v.text = "当前位置"
        v.font = 11.autoFontSize()
        v.textColor = UIColor(hex: 0x333333)
        return v
    }()
    
    let name: UILabel = {
        let v = UILabel()
        
        v.font = 13.autoFontSize()
        v.textColor = UIColor(hex: 0x141414)
        return v
    }()
    
    let address: UILabel = {
        let v = UILabel()
        
        v.font = 11.autoFontSize()
        v.textColor = UIColor(hex: 0x333333)
        return v
    }()
    
    var close: UIButton = {
        let v = UIButton()
        v.setImage(UIImage(named: "close_area_choice"), for: .normal)
        return v
    }()
    
    var locationPic: UIImageView = {
        let v = UIImageView()
        v.sy_name("location_area_choice")
        return v
    }()
    
    var params: [String : String] = [:]
    
    init(params: [String: String]) {
        super.init(nibName: nil, bundle: nil)
        self.params = params
        quxian.setTitle(params["name"], for: .normal)
        name.text = params["locationName"]
        address.text = params["address"]
        self.params.removeValue(forKey: "locationName")
        self.params.removeValue(forKey: "address")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initViews()
        getData(dic: params)
    }
    
    func getData(dic: [String : String]) {
        
        requestVM.baseRequest(disposeBag: dispose, type: .dy_firstPageLocation(dic: dic), modelClass: SGBaseModel<DYLocationChoiceModel>.self) {[weak self] (res) in
            if let `self` = self {
                self.model = res.data ?? DYLocationChoiceModel()
                let titles = self.model.sysDistrictsList?.map { "  \($0.name ?? "")  " } ?? []
                let _ = self.note.initViews(texts: titles)
            }
            
        } Error: {
            
        }
    }

    func initViews() {
        
        note.clickClosure = {[weak self] index in
            if let `self` = self {
                let gps =  self.model.sysDistrictsList?[index].centerCoordinate
                let locationName =  self.model.sysDistrictsList?[index].name
                self.passParamsClosure?((gps, locationName) as Any)
                self.popVC()
            }
        }
        quxian.addAction {[weak self] in
            if let `self` = self {
//                let gps =  self.model.sysDistrictsList?[index].centerCoordinate
//                self.passParamsClosure?(gps)
//                self.popVC()
            }
        }
        close.addAction {[weak self] in
            if let `self` = self {
                self.popVC()
            }
        }
        setSearchView()
        view.addSubview(note)
        view.addSubview(lineView)
        view.addSubview(secLineView)
        view.addSubview(jiedao)
        
        view.addSubview(quxian)
        view.addSubview(current)
        view.addSubview(name)
        view.addSubview(address)
        view.addSubview(close)
        view.addSubview(locationPic)
        close.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(STATUSBAR_HEIGHT)
            make.height.equalTo(48.autoWidth())
            make.width.equalTo(40.autoWidth())
        }
        lineView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(NAV_HEIGHT)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(0.5.autoWidth())
        }
        
        secLineView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(NAV_HEIGHT + 114.autoWidth())
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(0.5.autoWidth())
        }
        current.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(NAV_HEIGHT + 15.autoWidth())
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview()
            make.height.equalTo(11.autoWidth())
        }
        name.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(NAV_HEIGHT + 34.autoWidth())
            make.left.equalToSuperview().offset(30.autoWidth())
            make.right.equalToSuperview()
            make.height.equalTo(13.autoWidth())
        }
        address.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(NAV_HEIGHT + 54.autoWidth())
            make.left.equalToSuperview().offset(30.autoWidth())
            make.right.equalToSuperview()
            make.height.equalTo(12.autoWidth())
        }
        quxian.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(NAV_HEIGHT + 79.autoWidth())
            make.left.equalToSuperview().offset(15.autoWidth())
            make.height.equalTo(25.autoWidth())
            make.width.equalTo(65.autoWidth())
        }
        jiedao.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(NAV_HEIGHT + 124.autoWidth())
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview()
            make.height.equalTo(12.autoWidth())
        }
        locationPic.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(NAV_HEIGHT + 35.autoWidth())
            make.left.equalToSuperview().offset(15.autoWidth())
            make.height.equalTo(12.autoWidth())
            make.width.equalTo(12.autoWidth())
        }
        
        note.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(NAV_HEIGHT + 144.autoWidth())
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
    func setSearchView() {
        searchView = DYSearchView(frame: CGRect.init(x: 25.autoWidth(), y: STATUSBAR_HEIGHT + 10, width: SCREEN_WIDTH-25.autoWidth(), height: 28.0.autoWidth()))
        searchView.tf.placeholder = "搜索"
        searchView.close.isHidden = true
        searchView.clickChoiceClosure = {[weak self] index in
            if let `self` = self {
                if index == 0 {
//                    self.params = [:]
                } else {
//                    self.params = ["issue":self.searchView.tf.text ?? ""]
                }
//
            }
        }
        view.addSubview(searchView)
        
    }
}
